package 匿名函数

fun main() {
    val total = "Mississippi".count()
    println(total)

    val totalS = "Mississippi".count { letter ->
        letter == 's'
    }
    println(totalS)

    //变量的类型是一个匿名函数   无参数
    val blessingFunction: () -> String = {
        val holiday = "New Year."
        "Happy $holiday"
    }
    println(blessingFunction())

    //变量的类型是一个匿名函数   有参数
    val blessingFunction2: (String) -> String = { name ->
        val holiday = "New Year."
        "$name, Happy $holiday"
    }
    println(blessingFunction2("Jack"))

    //变量的类型是一个匿名函数   有参数  省略name  使用it关键字
    val blessingFunction3: (String) -> String = {
        val holiday = "New Year."
        "$it, Happy $holiday"
    }
    println(blessingFunction3("Jack"))


    val blessingFunction4 = {
        val holiday = "New Year."
        "Happy $holiday"
    }
    println(blessingFunction4())

    val blessingFunction5: (String, Int) -> String = { name, year ->
        val holiday = "New Year."
        "$name, Happy $holiday $year"
    }
    println(blessingFunction5("John", 33))

    val blessingFunction6 = { name: String, year: Int ->
        val holiday = "New Year."
        "$name, Happy $holiday $year"
    }
    println(blessingFunction6("Jack", 2027))
}
















